package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by balau on 3/10/14.
 *
 */

public class Menu {

    private final PrintStream out;
    private Library library;
    private BufferedReader in;
    private List<String> items;

    public Menu(Library library, PrintStream out, BufferedReader in) {
        this.out = out;
        this.library = library;
        this.in = in;
        this.items = new ArrayList<>();
        this.items.add("1 List books");
        this.items.add("2 Checkout book");
    }

    public void implementChoice(String menuChoice) throws IOException {
        if (menuChoice.equals("1")) {
            library.printBooks();
        } else if (menuChoice.equals("2")) {
            out.println("Please select a book to check out: ");
            String thisBook = in.readLine();
            out.println("Please enter your name: ");
            String thisUser = in.readLine();
            library.checkoutBook(thisBook,thisUser);
        }
    }

//    public boolean validateBookChoice(String bookChoice) {
//
//    }

    public boolean validateChoice(String menuChoice) {
        boolean isValid = false;
        if (menuChoice.equals("1")) {
            isValid = true;
        } else if (menuChoice.equals("2")) {
            isValid = true;
        }
        return isValid;
    }

    public void print() {
        out.println("Please select an option: ");
        for (String item : items) {
            out.println(item);
        }
        out.println();
    }


    public String getChoice() throws IOException {
        String menuChoice = in.readLine();
        while (!validateChoice(menuChoice)) {
            out.println("Select a valid option!");
            menuChoice = in.readLine();
        }
        return menuChoice;
    }
}